<template>
  <div class="formbox">
    <h3>后台管理系统</h3>
    <el-form
      ref="loginFormRef"
      style="max-width: 600px"
      :model="loginForm"
      status-icon
      :rules="rules"
      label-width="auto"
      class="demo-ruleForm"
    >
      <el-form-item label="名称" prop="username">
        <el-input v-model="loginForm.username" autocomplete="off" />
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          autocomplete="off"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          登录
        </el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script setup>
import { reactive, ref } from "vue";
import { useRouter } from "vue-router";
const router = useRouter();
const loginForm = reactive({
  username: "",
  password: "",
});
const loginFormRef = ref();
// 校验规则
const rules = {
  username: [
    { required: true, message: "请输入用户名", trigger: "blur" },
    { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" },
  ],
  password: [
    { required: true, message: "请输入密码", trigger: "blur" },
    { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" },
  ],
};
const submitForm = () => {
  // 登录按钮回调函数
  // 1验证表单
  loginFormRef.value.validate((valid) => {
    //valid是true或false
    if (!valid) return;
    if (valid) {
      console.log(loginForm);
      router.push("/home");
      // 2获取表单数据，传给后台
      // 3.设置token
      // 跳转页面
    }
  });
  // 2获取表单数据，传给后台
  // 设置token
};
</script>

<style lang="scss" scoped>
.formbox {
  h3 {
    margin-bottom: 30px;
  }
  width: 500px;
  height: 300px;
  position: fixed;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  background: rgba($color: #000000, $alpha: 0.5);
  color: white;
  text-align: center;
  padding: 20px;
}
::v-deep .el-form-item__label {
  color: white;
}
</style>
